package com.baidu.sggc.controller;

import com.baidu.sggc.audit.extend.AuditExtendContext;
import com.baidu.sggc.audit.extend.GeneralMessage;
import com.baidu.sggc.core.annotation.AuditLog;
import com.baidu.sggc.core.common.R;
import com.baidu.sggc.data.req.FileInfoCreateReq;
import com.baidu.sggc.data.req.FileInfoDelReq;
import com.baidu.sggc.data.req.FileInfoListReq;
import com.baidu.sggc.service.FileInfoService;
import com.baidu.sggc.entity.vo.FileInfoVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/fileInfo")
@Tag(name = "FileInfoController", description = "文件信息控制器")
public class FileInfoController {

  @Autowired
  private FileInfoService fileInfoService;

  @Operation(summary = "详情-新增文件信息")
  @PostMapping("/add")
  @ResponseBody
  @AuditLog(module = "文件管理", auditObject = "文件信息", operation = "create", message = "新增文件信息")
  public R<Boolean> add(@RequestBody FileInfoCreateReq req) {
    System.out.println("/fileInfo/add" + ": " + LocalDateTime.now());

    Long fileId = fileInfoService.createFileInfo(req);

    // 审计扩展字段
    GeneralMessage msg = new GeneralMessage();
    msg.setDataType("文件");
    msg.setDataName(req.getObject());
    msg.setUniqueIdentification(Collections.singletonList(fileId.toString())); // 用数据库生成的主键ID
    Map<String, String> attr = new HashMap<>();
    attr.put("文件大小", String.valueOf(req.getFileSize()));
    msg.setAttributeName(attr);
    msg.setOperationNum(1);
    AuditExtendContext.setExtendData(toMap(msg));

    return R.ok();
  }

  @Operation(summary = "详情-根据id获取文件信息")
  @GetMapping("/get")
  @ResponseBody
  @AuditLog(module = "文件管理", auditObject = "文件信息", operation = "detailQuery", message = "查看文件详情")
  public R<FileInfoVo> get(Long id) {
    System.out.println("/fileInfo/get" + ": " + LocalDateTime.now());
    if (id == null) {
      throw new IllegalArgumentException("id cannot be null");
    }

    // 审计扩展字段
    Map<String, Map<String, String>> extendData = new LinkedHashMap<String, Map<String, String>>();
    Map<String, String> condition = new LinkedHashMap<String, String>();
    condition.put("文件ID", String.valueOf(id));
    extendData.put("查询条件", condition);
    AuditExtendContext.setExtendData((Map) extendData);

    return R.ok(fileInfoService.getFileInfoById(id));
  }

  @Operation(summary = "详情-根据id删除文件信息")
  @PostMapping("/del")
  @ResponseBody
  @AuditLog(module = "文件管理", auditObject = "文件信息", operation = "delete", message = "删除文件信息")
  public R<Boolean> del(@RequestBody FileInfoDelReq req) {
    System.out.println("/fileInfo/del" + ": " + LocalDateTime.now());
    req.validate();

    // 审计扩展字段
    GeneralMessage msg = new GeneralMessage();
    msg.setDataType("文件");
    msg.setUniqueIdentification(Collections.singletonList(String.valueOf(req.getId())));
    msg.setOperationNum(1);
    AuditExtendContext.setExtendData(toMap(msg));

    return R.ok(fileInfoService.deleteFileInfoById(req));
  }

  @Operation(summary = "详情-查看文件列表")
  @PostMapping("/list")
  @ResponseBody
  @AuditLog(module = "文件管理", auditObject = "文件信息", operation = "listQuery", message = "查询文件信息列表")
  public R<List<FileInfoVo>> list(@RequestBody FileInfoListReq req) {
    System.out.println("/fileInfo/list" + ": " + LocalDateTime.now());
    req.validate();

    // 审计扩展字段
    Map<String, Map<String, String>> extendData = new LinkedHashMap<String, Map<String, String>>();

    Map<String, String> condition = new LinkedHashMap<String, String>();
    condition.put("关键字", String.valueOf(req.getBizId()));
    condition.put("文件类型", req.getBizType());
    extendData.put("查询条件", condition);

    Map<String, String> sort = new LinkedHashMap<String, String>();
    sort.put("上传时间", "降序");
    extendData.put("排序", sort);

    AuditExtendContext.setExtendData((Map) extendData);

    return R.ok(fileInfoService.getFileInfoList(req));
  }

  private Map<String, Object> toMap(Object obj) {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.convertValue(obj, Map.class);
  }
}
